package screens;

import com.rim.samples.device.push.PushController;

import log.Logger;
import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import util.LookAndFeel;
import beans.Course;
import beans.Notification;
import customfields.InformationField;
import customfields.NotificationField;

public class NotificationScreen extends TransitionScreen{

	private VerticalFieldManager vfm;

	public NotificationScreen(int transitionType){
		super(transitionType);
		// restore original icon
		HomeScreen.updateIcon(Bitmap.getBitmapResource("icon.png"));
		PushController.updateIndicator(false);
		if (Controller.getInstance().getNotifications().size()<=6){
			// Main vertical field manager
			vfm = new VerticalFieldManager() {
				protected void sublayout( int maxWidth, int maxHeight )
				{
					//set width
					int displayWidth = Display.getWidth();
					//set height
					int displayHeight = Display.getHeight();
					super.sublayout( displayWidth, displayHeight);
					setExtent( displayWidth, displayHeight);
					System.out.println(displayHeight + "" +displayWidth);
				}
			};
		} else {
			vfm = new VerticalFieldManager();
		}
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), background_bitmap.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		InformationField title = new InformationField("Notificaciones", LookAndFeel.icono_notificaciones, Field.FIELD_HCENTER, Color.WHITE);
		vfm.add(title);
		vfm.add(new LabelField(""));

		loadNotifications();
		add(vfm);
	}

	public void loadNotifications(){
		//TODO fake notifications remove after testing
		//				Vector notifications = new Vector();
		//				notifications.addElement(new Notification(1, "128406", "", "", "", "fecha", "", 1));
		//		notifications.addElement(new Notification(2, "126498", "", "", "", "fecha", "", 1));
		//		notifications.addElement(new Notification(3, "126655", "", "", "", "fecha", "", 1));
		//		notifications.addElement(new Notification(4, "130307", "", "", "", "fecha", "", 1));
		//		notifications.addElement(new Notification(5, "114589", "", "", "", "fecha", "", 1));
		//		for(int i=notifications.size()-1; i>=0; i--){
		//			Background back = BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
		//					LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color);;
		//					Notification n = (Notification) notifications.elementAt(i);
		//					NotificationField nf = new NotificationField(n);
		//					nf.setBackground(back);
		//					nf.setChangeListener(notificationListener);
		//					nf.setCookie(n);
		//					vfm.add(nf);
		//		}

		//		Vector notifications2 = new Vector();
		//		notifications2.addElement(new Notification(1, "128406", "", "", "", "fecha1", "", 1));
		//		notifications2.addElement(new Notification(2, "126498", "", "", "", "fecha2", "", 1));
		//		notifications2.addElement(new Notification(3, "126655", "", "", "", "fecha3", "", 1));
		//		notifications2.addElement(new Notification(4, "130307", "", "", "", "fecha4", "", 1));
		//		notifications2.addElement(new Notification(5, "114589", "", "", "", "fecha5", "", 1));
		//		notifications2.addElement(new Notification(1, "128406", "", "", "", "fecha6", "", 1));
		//		notifications2.addElement(new Notification(2, "126498", "", "", "", "fecha7", "", 1));
		//		notifications2.addElement(new Notification(3, "126655", "", "", "", "fecha8", "", 1));
		//		notifications2.addElement(new Notification(4, "130307", "", "", "", "fecha9", "", 1));
		//		notifications2.addElement(new Notification(5, "114589", "", "", "", "fecha10", "", 1));
		//		notifications2.addElement(new Notification(1, "128406", "", "", "", "fecha11", "", 1));
		//		notifications2.addElement(new Notification(2, "126498", "", "", "", "fecha12", "", 1));
		//		notifications2.addElement(new Notification(3, "126655", "", "", "", "fecha13", "", 1));
		//		notifications2.addElement(new Notification(4, "130307", "", "", "", "fecha14", "", 1));
		//		notifications2.addElement(new Notification(5, "114589", "", "", "", "fecha15", "", 1));
		//		Controller.getInstance().addNotifications(notifications2);
		//TODO end remove

		// real version
		if (Controller.getInstance().getNotifications().isEmpty()){
			LabelField lf = new LabelField("No hay notificaciones", Field.FIELD_HCENTER);
			vfm.add(lf);
		} else {
			for(int i=Controller.getInstance().getNotifications().size()-1; i>=0; i--){
				Notification n = (Notification) Controller.getInstance().getNotifications().elementAt(i);
				NotificationField nf = new NotificationField(n);
				Background back = BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
						LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color);
				nf.setBackground(back);
				nf.setChangeListener(notificationListener);
				nf.setCookie(n);
				vfm.add(nf);
			}
		} 
	}

	private FieldChangeListener notificationListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			final Notification n = (Notification) field.getCookie();
			UiApplication.getUiApplication().invokeAndWait(new Runnable() {
				public void run() {
					Course c = Controller.getInstance().getCourseFromId(n.getIdAula());
					if (c==null){
						//TODO handle
						Logger.log("Could not find course for notification id: "+n.getIdAula());
					} else {
						Logger.log("Notification course: "+c.getName());
						String description = n.getDescription(c.getName());

						int answer = Dialog.ask(description, new String[] {"Ver","Salir"}, new int[] {Dialog.OK,Dialog.CANCEL}, Dialog.CANCEL);

						if (answer == Dialog.OK){
							// show course
							Controller.getInstance().openGradesListScreen(c);
						} else {
							// do nothing
						}
					}
				}
			});
		}
	};

}
